import { defineStore } from "pinia";
import { getservertype, excellentServicePersons, getdemand } from "@/api/home";
import { ref } from "vue";

export const uerPersonStore = defineStore(
  "Big-person",
  () => {
    const servertype = ref([]);
    const getServerType = async () => {
      try {
        const res: any = await getservertype();
        if (res && res.data) {
          servertype.value = res.data;
        } else {
          console.warn("服务类型数据为空");
          servertype.value = [];
        }
        return servertype.value;
      } catch (error) {
        console.error("获取服务类型失败：", error);
        servertype.value = [];
        return [];
      }
    };

    const serverperson = ref([]);
    const getserverperson = async () => {
      try {
        // 每次获取数据前先清空
        serverperson.value = [];
        const res: any = await excellentServicePersons();
        if (res && res.data) {
          serverperson.value = res.data;
        } else {
          console.warn("服务人员数据为空");
          serverperson.value = [];
        }
      } catch (error) {
        console.error("获取服务人员数据失败：", error);
        serverperson.value = [];
      }
      return serverperson.value;
    };

    const demand = ref([]);
    const getDemand = async () => {
      try {
        // 每次获取数据前先清空
        demand.value = [];
        const res: any = await getdemand();
        if (res && res.data) {
          demand.value = res.data;
        } else {
          console.warn("需求数据为空");
          demand.value = [];
        }
      } catch (error) {
        console.error("获取需求数据失败：", error);
        demand.value = [];
      }
    };
    //清空
    const reset = () => {
      servertype.value = [];
      serverperson.value = [];
      demand.value = [];
    };
    return {
      servertype,
      getServerType,
      serverperson,
      getserverperson,
      demand,
      getDemand,
      reset,
    };
  },
  {
    persist: true,
  }
);
